package com.doge.calcalbum.business.skin

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.doge.calcalbum.R
import com.doge.calcalbum.core.base.adapters.AdapterCallback
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.databinding.CellSkinBinding
import com.doge.calcalbum.utils.getColor
import com.doge.calcalbum.utils.setOnClickCallback

class SkinViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellSkinBinding = CellSkinBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false
    ),
) : RecyclerView.ViewHolder(binding.root) {
    init {
        binding.item.setOnClickCallback {
            callback.invoke(ITEM_CLICKED, itemView.tag)
        }
        binding.use.setOnClickCallback {
            callback.invoke(ITEM_CLICKED, itemView.tag)
        }
    }

    fun bind(item: Skin) {
        itemView.tag = item
        binding.cover.setImageResource(item.iconResId)
        binding.title.setText(item.textId)
        val using = item.id == SkinHelper.usingSkinId
        binding.usingText.isVisible = using
        binding.useText.isVisible = !using

        val skin = item.type == Skin.TYPE_SKIN
        binding.desc.text = item.type
        binding.desc.setBackgroundResource(if (skin) R.drawable.shape_ff924a_2_stroke else R.drawable.shape_7f3369ff_2_stroke)
        binding.desc.setTextColor(getColor(if (skin) "#FF924A" else "#3369FF"))
    }
}